import { DataCtr } from "../../../common/util/DataCtr";
import HollowOut from "./HollowOut";
import TouchBlocker from "./TouchBlocker";

const { ccclass, property } = cc._decorator;

export interface taskData {
    cmd: string,                //引导类型
    args: string,               //参数，假如是text类型：内容是文字，如果是click,内容是路径
    c?: number | null,         //延迟时间
    text?: string | null,              //提示文字
    textPos?: cc.Vec2 | null           //提示文字的位置

}



@ccclass
export default class DlgGuide extends cc.Component {
    // public static pUrl: string = 'LyDialog/DlgGuide'
    @property(HollowOut)
    hollowOut: HollowOut = null
    @property(TouchBlocker)
    touchBlocker: TouchBlocker = null
    @property(cc.Node)
    textNode: cc.Node = null
    @property(cc.Label)                 //点击的按钮提示
    clickText: cc.Label = null

    textLab: cc.Label = null        //text lab
    task: taskData[] = []           //任务列表
    inGuide: boolean = false           //是否开始新手引导
    nowIndex: number = 0              //当前任务index
    // LIFE-CYCLE CALLBACKS:



    // update (dt) {}

    init() {
        this.textLab = this.textNode.getChildByName('textLab').getComponent(cc.Label)

    }

    /**读取预设定的引导流程 */
    startLocalGuide(data) {
        this.task = data
        this.nowIndex = 0
        // 屏蔽所有点击
        this.touchBlocker.blockAll();
    }
    /**读取配置表里的新手引导任务 */
    getExcelGuide() {
        this.task = DataCtr.getTable('Guide')
        this.nowIndex = 0
    }
    /**开始新手引导 */
    startGuide() {
        this.inGuide = true
        this.checkGuide()
    }


    /**遍历当前nowIndex的任务 */
    checkGuide() {
        //不在引导流程中或者已经遍历全部任务则结束流程
        if (!this.inGuide || this.nowIndex >= this.task.length) return
        switch (this.task[this.nowIndex].cmd) {
            case 'click':
                this.clickServer(this.task[this.nowIndex])
                break
            case 'text':
                this.textServer(this.task[this.nowIndex])
                break
        }
    }
    /**点击事件处理 */
    async clickServer(data: taskData) {
        this.hollowOut.node.active = true
        const node = cc.find(data.args)
        //遮罩
        await this.hollowOut.rectTo(0.5, node.getPosition(), node.width + 10, node.height + 10, 5, 5);
        //展示点击提示
        if (data.text != null) {
            this.clickText.node.active = true
            this.clickText.string = `${data.text}`
            this.clickText.node.setPosition(data.textPos ?? cc.v2(0, 0))
        } else {
            this.clickText.node.active = false
        }
        node.once(cc.Node.EventType.TOUCH_END, () => {
            this.hollowOut.node.active = false
            this.nowIndex += 1
            this.checkGuide()
        });
        this.touchBlocker.setTarget(node);
    }

    textServer(data: taskData) {
        this.textNode.active = true
        this.textLab.string = `${data.args}`
        this.textNode.once(cc.Node.EventType.TOUCH_END, () => {
            this.nowIndex += 1
            this.textNode.active=false
            this.checkGuide()
        });
    }

    /**关闭新手引导 */
    close() {
        this.inGuide = false
        this.hollowOut.node.active = false
        this.textLab.node.active = false
        this.textNode.active = false
    }



}
